//
//  SmartRoadShowAppDelegate.m
//  SmartRoadShow
//
//  Created by simon on 10-9-16.
//  Copyright simonwang142232@gmail.com 2010. All rights reserved.
//

#import "SmartRoadShowAppDelegate.h"
#import "MyTabBar.h"
#import "MyTabBarItem.h"
#import "MyViewController.h"
#import "MyTabBarController.h"
#import "CustomButton.h"
#import "MyNavigationController.h"
#import "RS1ViewController.h"
#import "RS2ViewController.h"
#import "RS3ViewController.h"
#import "RS4ViewController.h"
#import "RS5ViewController.h"
#import "DefaultViewController.h"
#import "CalendarViewController.h"
#import "DetailWebViewController.h"
#import "extev.h"
#import "InfoMgr.h"

InfoMgr *notiInfo;

@interface SmartRoadShowAppDelegate() 
- (void)sendProviderDeviceToken:(NSData *)devToken;
@end


@implementation SmartRoadShowAppDelegate

@synthesize window, tabBarController, defaultVC, _calendarViewController, navigationController_calendar, rs1VC,rs2VC, rs3VC, rs5VC, token;
@synthesize planViewController;
#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

	[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];
	
	 
	[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
	

	notiInfo = [[InfoMgr alloc]init];
	[notiInfo loadDataList];
	[notiInfo release];
	//tabBarController
	tabBarController = [[MyTabBarController alloc] init];
	tabBarController.wantsFullScreenLayout = YES;

	//都市新主张
	rs3VC = [[RS3ViewController alloc] init];
	MyNavigationController* navigationController3 = [[MyNavigationController alloc] initWithRootViewController:rs3VC];
	navigationController3.tabBarItem.title = @"城市大不同";
	navigationController3.tabBarItem.image = [UIImage imageNamed:@"tab2.png"];
	navigationController3.tabBarItem.pressedImage = [UIImage imageNamed:@"tab2_selected.png"];	
	navigationController3.title = @"城市大不同";
	rs3VC.navigationController = navigationController3;
	
	UIButton *overview3 = [UIButton buttonWithType:UIButtonTypeCustom];
	[overview3 setTag:803];
	[overview3.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
	[overview3 setTitle:@"概述" forState:UIControlStateNormal];
	[overview3 setBackgroundImage:[UIImage imageNamed:@"overview.png"] forState:UIControlStateNormal];
	[overview3 addTarget:self action:@selector(seeOverview:) forControlEvents:UIControlEventTouchUpInside];
	[overview3 setFrame:CGRectMake(250, 8, 56, 28)];
	[navigationController3.navigationBar addSubview:overview3];
	
	
	//艺术地图
	rs1VC = [[RS1ViewController alloc] init];
	MyNavigationController* navigationController1 = [[MyNavigationController alloc] initWithRootViewController:rs1VC];
	navigationController1.tabBarItem.title = @"艺术地图";
	navigationController1.tabBarItem.image = [UIImage imageNamed:@"tab1.png"];
	navigationController1.tabBarItem.pressedImage = [UIImage imageNamed:@"tab1_selected.png"];	
	navigationController1.title = @"艺术地图";
	rs1VC.navigationController = navigationController1;	
	
	//艺术地图“概述”按钮
	UIButton *overview1 = [UIButton buttonWithType:UIButtonTypeCustom];
	[overview1 setTag:801];
	[overview1.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
	[overview1 setTitle:@"概述" forState:UIControlStateNormal];
	[overview1 setBackgroundImage:[UIImage imageNamed:@"overview.png"] forState:UIControlStateNormal];
	[overview1 addTarget:self action:@selector(seeOverview:) forControlEvents:UIControlEventTouchUpInside];
	[overview1 setFrame:CGRectMake(250, 8, 56, 28)];
	[navigationController1.navigationBar addSubview:overview1];
	
	//在线购车
//	rs2VC = [[RS2ViewController alloc] init];
//	MyNavigationController* navigationController2 = [[MyNavigationController alloc] initWithRootViewController:rs2VC];
//	navigationController2.tabBarItem.title = @"我要买车";
//	navigationController2.tabBarItem.image = [UIImage imageNamed:@"tab3.png"];
//	navigationController2.tabBarItem.pressedImage = [UIImage imageNamed:@"tab3_selected.png"];	
//	navigationController2.title = @"我要买车";
//	rs2VC.navigationController = navigationController2;

	
	//联系我们
	RS4ViewController* rs4VC = [[RS4ViewController alloc] init];
	MyNavigationController* navigationController4 = [[MyNavigationController alloc] initWithRootViewController:rs4VC];
	navigationController4.tabBarItem.title = @"联系我们";
	navigationController4.tabBarItem.image = [UIImage imageNamed:@"tab4.png"];
	navigationController4.tabBarItem.pressedImage = [UIImage imageNamed:@"tab4_selected.png"];
	navigationController4.title = @"联系我们";
	//rs4VC.navigationController = navigationController4;
	
	//精彩分享
	rs5VC = [[RS5ViewController alloc] init];
	MyNavigationController* navigationController5 = [[MyNavigationController alloc] initWithRootViewController:rs5VC];
	navigationController5.tabBarItem.title = @"精彩分享";
	navigationController5.tabBarItem.image = [UIImage imageNamed:@"tab5.png"];
	navigationController5.tabBarItem.pressedImage = [UIImage imageNamed:@"tab5_selected.png"];
	navigationController5.title = @"精彩分享";
	
	tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController3,navigationController1,
										navigationController4, navigationController5,nil];
	
	
	[rs1VC release];
	[navigationController1 release];
//	[rs2VC release];
//	[navigationController2 release];
	
	[rs3VC release];
	[navigationController3 release];
	
	[rs4VC release];
	[navigationController4 release];
	[rs5VC release];
	[navigationController5 release];
	
	
	//首页
	defaultVC = [[DefaultViewController alloc] init];
	tabBarController.defaultController = defaultVC;
	
	//日历
	_calendarViewController = [[CalendarViewController alloc] init];
	navigationController_calendar = [[MyNavigationController alloc] initWithRootViewController:_calendarViewController];
	navigationController_calendar.title = @"活动日历";
	navigationController_calendar.tabBarController = tabBarController;
	_calendarViewController.navigationController = navigationController_calendar;
	
	
	//金融计划
	
	
	DetailWebViewController *detailView = [[DetailWebViewController alloc] init];
	
	NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/plan/plan.html"];
	
	detailView.webView.frame = CGRectMake(5, 5, 310, 330);
	
	[[detailView webView] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
	
	planViewController = [[MyNavigationController alloc] initWithRootViewController:detailView];
	planViewController.title = @"金融计划";
	planViewController.tabBarController = tabBarController;
	detailView.navigationController = planViewController;
	[detailView release];
	
	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	
	[window addSubview:tabBarController.view];
    [window makeKeyAndVisible];	
	return YES;
	
	
	
}

- (void)applicationDidBecomeActive:(UIApplication *)application{
	[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

- (void)applicationWillEnterForeground:(UIApplication *)application{
	[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

//概述
- (void)seeOverview:(UIButton *) button{
	
	overviewController = [[DetailWebViewController alloc] init];
	if (button.tag == 801) {
		//艺术地图的概述
		NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/map_overview/overview1.html"];
		[[overviewController webView] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
		[rs1VC.navigationController pushSoloViewController:overviewController];
	}
	else {
		//都市新主张的概述
		NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/opinions/opinions.html"];
		[[overviewController webView] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
		[rs3VC.navigationController pushSoloViewController:overviewController];
	}
	[overviewController release];
	
}

//PUSH FUNCTION
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
	
	//NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
	
	//int i = [[userDefaults objectForKey:@"push"] intValue];
	//[userDefaults setInteger:++i forKey:@"push"];
	//if(i == 1){
	[self sendProviderDeviceToken:devToken];
	//NSLog(@"token=%@", [devToken description]);
	//}
}

- (void)sendProviderDeviceToken:(NSData *)devToken {
	
	NSString *token_string = [[[devToken description] 
							   stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] 
							  stringByReplacingOccurrencesOfString:@" " withString:@""];	
	self.token = token_string;
	
	//post
	NSString *url_string = kTokenSaveURL;	
	NSURL *url= [NSURL URLWithString:url_string];
	NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
	[request setTimeoutInterval: 30];
	
	[request setHTTPMethod:@"POST"];
	
	NSString* postStr = nil;
	
	if (![[[NSUserDefaults standardUserDefaults] objectForKey:@"saveid"] isEqualToString:@"1"]) {
		 postStr = [NSString stringWithFormat:@"token=%@&saveid=0", token_string];
		[[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"saveid"];
		
		NSLog(@" == 0");
	}
	else {
		 postStr = [NSString stringWithFormat:@"token=%@&saveid=1", token_string];
		
		NSLog(@" == 1");
	}

	
	
	NSData* postData = [postStr dataUsingEncoding:NSUTF8StringEncoding];
	[request setHTTPBody:postData];
	
	
	NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
	[connection release];
	[request release];
	
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
	NSLog(@"%@ %d",userInfo,__LINE__);
    
	for(NSString* key in [userInfo allKeys]){
		NSLog(@"key:%@, value:%@", key, [userInfo objectForKey:key]);
		NSLog(@"alert:%@", [[userInfo objectForKey:key] objectForKey:@"alert"]);
	}
	UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"都市艺术指南" message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
	[alertView show];
	[alertView release];
}


- (void)dealloc {
    [window release];
	[tabBarController release];
	[defaultVC release];
	[_calendarViewController release];
	[planViewController release];
    [super dealloc];
}


@end
